package net.zoneland.zrdp.web.controller.monitor;

import java.util.*;

import com.fasterxml.jackson.core.JsonProcessingException;

import net.zoneland.zrdp.common.annotation.Log;
import net.zoneland.zrdp.common.constant.CacheConstants;
import net.zoneland.zrdp.common.core.controller.BaseController;
import net.zoneland.zrdp.common.core.domain.AjaxResult;
import net.zoneland.zrdp.common.core.domain.model.LoginUser;
import net.zoneland.zrdp.common.core.page.TableDataInfo;
import net.zoneland.zrdp.common.core.redis.RedisService;
import net.zoneland.zrdp.common.enums.BusinessType;
import net.zoneland.zrdp.common.utils.JacksonUtils;
import net.zoneland.zrdp.framework.web.service.TokenService;
import net.zoneland.zrdp.system.domain.SysUserOnline;
import net.zoneland.zrdp.system.service.ISysUserOnlineService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 在线用户监控
 *
 * @author zonevue
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {
    @Resource
    private ISysUserOnlineService userOnlineService;

    @Resource
    private TokenService tokenService;

    @Resource
    private RedisService redisService;

    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/list")
    public TableDataInfo list(final String ipaddr, final String userName) throws JsonProcessingException {
        final Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        final List<SysUserOnline> userOnlineList = new ArrayList<>();
        for (final String key : keys) {
            final LoginUser user = JacksonUtils.toObject(redisService.getCacheString(key),LoginUser.class);
            if (StringUtils.isNoneBlank(ipaddr, userName)) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
            } else if (StringUtils.isNotBlank(ipaddr)) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
            } else if (StringUtils.isNotBlank(userName) && Objects.nonNull(user.getUser())) {
                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
            } else {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }

    /**
     * 强退用户
     */
    @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable final String tokenId) {
        redisService.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        tokenService.evictUserCacheOnJvm(tokenId);
        return success();
    }
}
